import sys

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QDialog, QMessageBox, QApplication

from backend.db.database import SessionLocal
from backend.services.consumption_services import ConsumptionService
from front.ui.ui_recharge_dialog import Ui_Dialog


class RechargeDialog(QDialog, Ui_Dialog):
    refresh=Signal()
    def __init__(self , parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.doubleSpinBox.setRange(0, 10000)
        self.pushButton.clicked.connect(self.recharge_amount)

    def recharge_amount(self):
        try:
            with SessionLocal() as session:
                consumption_service=ConsumptionService(session)
                vip_id = self.lineEdit_3.text().strip()
                amount = self.doubleSpinBox.value()
                description = self.lineEdit_4.text().strip()
                if not vip_id:
                    QMessageBox.warning(self, "警告", "缺少ID")
                    return
                consumption_service.add_recharge(vip_id, amount, description)
                QMessageBox.information(self, "成功", "充值成功")
                self.refresh.emit()
                self.deleteLater()
        except ValueError as e:
            QMessageBox.warning(self, "警告", "无此会员")


if __name__ == '__main__':
    app = QApplication([])

    dialog = RechargeDialog()
    dialog.show()
    sys.exit(app.exec())
